namespace ASPNET.StarterKit.IssueTracker
{
    using ASPNET.StarterKit.IssueTracker.BusinessLogicLayer;
    using ASPNET.StarterKit.IssueTracker.UserInterfaceLayer;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Diagnostics;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class ProjectCustomFields : UserControl, IEditProjectControl
    {
        [AccessedThroughProperty("btnAdd")]
        private Button _btnAdd;
        [AccessedThroughProperty("chkRequired")]
        private CheckBox _chkRequired;
        [AccessedThroughProperty("dropDataType")]
        private DropDownList _dropDataType;
        [AccessedThroughProperty("grdCustomFields")]
        private DataGrid _grdCustomFields;
        [AccessedThroughProperty("lblError")]
        private Label _lblError;
        private int _ProjectId;
        [AccessedThroughProperty("txtName")]
        private TextBox _txtName;
        private object designerPlaceholderDeclaration;

        public ProjectCustomFields()
        {
            base.Init += new EventHandler(this.Page_Init);
            this._ProjectId = -1;
        }

        public void AddCustomField(object s, EventArgs e)
        {
            string newName = this.txtName.Text.Trim();
            if (StringType.StrCmp(newName, string.Empty, false) != 0)
            {
                ValidationDataType dataType = (ValidationDataType) IntegerType.FromObject(Enum.Parse(typeof(ValidationDataType), this.dropDataType.SelectedValue));
                bool required = this.chkRequired.Checked;
                CustomField newCustomField = new CustomField(this.ProjectId, newName, dataType, required);
                if (newCustomField.Save())
                {
                    this.txtName.Text = "";
                    this.dropDataType.SelectedIndex = 0;
                    this.chkRequired.Checked = false;
                    this.BindCustomFields();
                }
                else
                {
                    this.lblError.Text = "Could not save custom field";
                }
            }
        }

        public void BindCustomFields()
        {
            this.grdCustomFields.DataSource = CustomField.GetCustomFieldsByProjectId(this.ProjectId);
            this.grdCustomFields.DataKeyField = "Id";
            this.grdCustomFields.DataBind();
            if (this.grdCustomFields.Items.Count == 0)
            {
                this.grdCustomFields.Visible = false;
            }
            else
            {
                this.grdCustomFields.Visible = true;
            }
        }

        public void DeleteCustomField(object s, DataGridCommandEventArgs e)
        {
            if (!CustomField.DeleteCustomField(IntegerType.FromObject(this.grdCustomFields.DataKeys[e.Item.ItemIndex])))
            {
                this.lblError.Text = "Could not delete custom field";
            }
            else
            {
                this.BindCustomFields();
            }
        }

        public void grdCustomFields_ItemDataBound(object s, DataGridItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) | (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                CustomField currentCustomField = (CustomField) e.Item.DataItem;
                Label lblName = (Label) e.Item.FindControl("lblName");
                lblName.Text = currentCustomField.Name;
                Label lblDataType = (Label) e.Item.FindControl("lblDataType");
                lblDataType.Text = currentCustomField.DataType.ToString();
                Label lblRequired = (Label) e.Item.FindControl("lblRequired");
                lblRequired.Text = currentCustomField.Required.ToString();
                Button btnDelete = (Button) e.Item.FindControl("btnDelete");
                btnDelete.Attributes.Add("onclick", string.Format("return confirm('Are you sure you want to delete the \"{0}\" field?');", currentCustomField.Name));
            }
        }

        public void Initialize()
        {
            this.dropDataType.DataSource = Enum.GetNames(typeof(ValidationDataType));
            this.dropDataType.DataBind();
            this.BindCustomFields();
        }

        [DebuggerStepThrough]
        private void InitializeComponent()
        {
        }

        private void Page_Init(object sender, EventArgs e)
        {
            this.InitializeComponent();
        }

        public bool Update()
        {
            return true;
        }

        int ASPNET.StarterKit.IssueTracker.UserInterfaceLayer.IEditProjectControl.ProjectId
        {
            get
            {
                return this._ProjectId;
            }
            set
            {
                this._ProjectId = value;
            }
        }

        protected virtual Button btnAdd
        {
            get
            {
                return this._btnAdd;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._btnAdd != null)
                {
                    this._btnAdd.Click -= new EventHandler(this.AddCustomField);
                }
                this._btnAdd = value;
                if (this._btnAdd != null)
                {
                    this._btnAdd.Click += new EventHandler(this.AddCustomField);
                }
            }
        }

        protected virtual CheckBox chkRequired
        {
            get
            {
                return this._chkRequired;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._chkRequired != null)
                {
                }
                this._chkRequired = value;
                if (this._chkRequired != null)
                {
                }
            }
        }

        protected virtual DropDownList dropDataType
        {
            get
            {
                return this._dropDataType;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._dropDataType != null)
                {
                }
                this._dropDataType = value;
                if (this._dropDataType != null)
                {
                }
            }
        }

        protected virtual DataGrid grdCustomFields
        {
            get
            {
                return this._grdCustomFields;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._grdCustomFields != null)
                {
                    this._grdCustomFields.ItemDataBound -= new DataGridItemEventHandler(this.grdCustomFields_ItemDataBound);
                    this._grdCustomFields.DeleteCommand -= new DataGridCommandEventHandler(this.DeleteCustomField);
                }
                this._grdCustomFields = value;
                if (this._grdCustomFields != null)
                {
                    this._grdCustomFields.ItemDataBound += new DataGridItemEventHandler(this.grdCustomFields_ItemDataBound);
                    this._grdCustomFields.DeleteCommand += new DataGridCommandEventHandler(this.DeleteCustomField);
                }
            }
        }

        protected virtual Label lblError
        {
            get
            {
                return this._lblError;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._lblError != null)
                {
                }
                this._lblError = value;
                if (this._lblError != null)
                {
                }
            }
        }

        public int ProjectId
        {
            get
            {
                return this._ProjectId;
            }
            set
            {
                this._ProjectId = value;
            }
        }

        protected virtual TextBox txtName
        {
            get
            {
                return this._txtName;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._txtName != null)
                {
                }
                this._txtName = value;
                if (this._txtName != null)
                {
                }
            }
        }
    }
}

